/*************************************************************************
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *
 *                COPYRIGHT (C) HONGLING CAPITAL CORPORATION 2012
 *    ALL RIGHTS RESERVED BY HONGLING CAPITAL CORPORATION. THIS PROGRAM
 * MUST BE USED  SOLELY FOR THE PURPOSE FOR WHICH IT WAS FURNISHED BY
 * HONGLING CAPITAL CORPORATION. NO PART OF THIS PROGRAM MAY BE REPRODUCED
 * OR DISCLOSED TO OTHERS,IN ANY FORM, WITHOUT THE PRIOR WRITTEN
 * PERMISSION OF HONGLING CAPITAL CORPORATION. USE OF COPYRIGHT NOTICE
 DOES NOT EVIDENCE PUBLICATION OF THE PROGRAM.
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *************************************************************************/

package com.springboot.common.jms;

import com.alibaba.fastjson.JSONObject;
import com.springboot.common.entity.BookInfo;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * activemq的消息发送者。
 *
 * @author hongchao zhao at 2019-5-23 16:30
 */
@Service
public class QueueMessage {

    private static Logger LOG = LoggerFactory.getLogger(QueueMessage.class);

    @Resource(name = "jmsTemplate")
    private JmsTemplate jmsTemplate;

    //定义消息队列
    @Resource(name = "queueDestination")
    private Destination destination;

    /**
     * 更新帖子的浏览量或是帖子回复量
     */
    public final static String UPDATE_TOPIC_READ_REPLY_COUNT = "UPDATE_TOPIC_READ_REPLY_COUNT";

    /**
     * 向指定Destination发送map异步消息
     *
     * @param messageKey 消息名称。
     * @param param      消息参数。
     */
    public void sendMapMessage(final String messageKey, Map<String, Object> param) {
        try {
            ActiveMQDestination activeMQDestination = (ActiveMQDestination) destination;
            //获取当前队列数目
            int count = activeMQDestination.getCompositeDestinations().length;
            //随机从两个队列中取出一个队列来发送消息【定义多重队列随机分配来消耗高并发数据量】
            ActiveMQDestination currentQueue = activeMQDestination.getCompositeDestinations()[RandomUtils.nextInt(0,count)];

            //将请求参数转换为JSON字符串
            final String message = JSONObject.toJSON(param).toString();
            LOG.debug("【{}】 向队列【{}】发送map消息【{}】。", Thread.currentThread().getName(), currentQueue, message);
            jmsTemplate.send(currentQueue, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    MapMessage mapMessage = session.createMapMessage();
                    //方法请求参数，以JSON格式来存储
                    mapMessage.setString("data", message);
                    //用于区分方法请求
                    mapMessage.setString("key", messageKey);
                    return mapMessage;
                }
            });
        } catch (Exception e) {
            LOG.error("向avtiveMq队列发送异步消息异常：" + e.getLocalizedMessage());
        }

    }



}
